#include "CPolygone.h"

#include "utils.h"



///////////////////////////////////
// AddPoint
//
// make the new line according 
// to a point in an unfinished Poly
////////////////////////////////////
void CPolygone::AddPoint(POINT p)
{
	bool last = false ;

	// if that is not a new point
	if ( p.x == lastpoint.x && p.y == lastpoint.y )
		return ;

	// if it is the first point
	if ( isClean == true )
	{
		begin = p ;
		isClean = false ;
	}
	else
	{

		// Check if the last point is close to
		// the first : if that's true it is the 
		// last point
		if ( CUtils::dist(p,begin) < 30 )
		{
			p = begin    ;
			last = true  ;
		}

		// Add the new line in
		// the listline of the Poly
		CLine line ;
		line.makeLine(lastpoint,p,SOLID);

		listline.push_front(line);

	}
	// save the point as the last
	lastpoint = p ;

	// if it is not the last point
	if ( last == false )
	{
		// To calculate the center
		nCorner++;
		totalx += p.x ;
		totaly += p.y ;
		// Add the point in the listpoint
		// of the Poly
		listpoint.push_front(p);
	}
	else
		MakeCorner() ;

}

//////////////////////////////
// Draw 
//
// Draw the lines of the Poly
/////////////////////////////
void CPolygone::Draw(HDC hdc)
{
	for ( iterline = listline.begin() ; iterline != listline.end() ; iterline++)
	{
		iterline->Draw(hdc);
	}
}

////////////////////////////
// Clear
//
// Clear the poly in order to
// create another
////////////////////////////
void CPolygone::clear()
{
	totalx = 0 ;
	totaly = 0 ;
	nCorner = 0;

	listline.clear() ;
	listpoint.clear();

	isClean = true ;
}

///////////////////////////
// Make Corner
//
// Find the center of a Poly
// and make the diagonals
///////////////////////////
void CPolygone::MakeCorner()
{

	center.x = totalx/nCorner ; 
	center.y = totaly/nCorner ;	
	
	// Place the corner points
	PlacePoints(15);

	// make an unpassable segment
	// between center and Corners
	list<POINT>::iterator iterpoint;
	for ( iterpoint = listpoint.begin() ; iterpoint != listpoint.end() ; iterpoint++)
	{
		CLine line ;
		line.makeLine(center,*iterpoint,SOLID);

		listline.push_front(line);

		// To avoid that the points touch the line
		if ( center.x < iterpoint->x )
			iterpoint->x++ ;
		else
			iterpoint->x-- ;
		
		if ( center.y < iterpoint->y )
			iterpoint->y++ ;
		else
			iterpoint->y-- ;
	
	}

}

//////////////////////////////////
// PlacePoints
//
// place the points of a Poly
// in a d distance of the corners
/////////////////////////////////
void CPolygone::PlacePoints(long d)
{

	list<POINT>::iterator iterpoint;

	for ( iterpoint = listpoint.begin() ; iterpoint != listpoint.end() ; iterpoint++)
	{
		bool negative = false ;
		POINT A,B ;
		CLine line ;
		line.makeLine(center,*iterpoint,SOLID);

		if ( iterpoint->x > center.x )
			A.x = iterpoint->x + 5 ;
		else
		{
			A.x = iterpoint->x - 5 ;
			negative = true ;
		}
		A.y = iterpoint->y ;

		B.x = A.x ;
		B.y = (long)( line.Geta() * B.x + line.Getb() ) ;

		long z = (long) ( CUtils::dist(*iterpoint,B) );

		if (negative)
			iterpoint->x = (long)( iterpoint->x + (-CUtils::dist(A,*iterpoint) * d )/z ) ;
		else
			iterpoint->x = (long)( iterpoint->x + ( CUtils::dist(A,*iterpoint) * d )/z ) ;

		iterpoint->y = (long)( line.Geta() * iterpoint->x  + line.Getb() ) ;

		
	}



}












